package com.example.qqemailtest.service.impl;

import com.example.qqemailtest.bean.User;
import com.example.qqemailtest.mapper.RegisterMapper;
import com.example.qqemailtest.service.RegistryService;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.annotations.Mapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

@Service
@Slf4j
public class RegistryServiceImpl implements RegistryService {
    @Autowired
    RegisterMapper registerMapper;
    @Autowired
    RedisTemplate redisTemplate;

    @Override
    public int registry(User user) {
   //判断注册码和qq邮箱是否相匹配
        Object o = redisTemplate.opsForValue().get(user.getVcode());
        if (!user.getEmail().equals(o)){
//           return "注册码不正确或者邮箱不正确";
            log.info("注册码不正确或者邮箱不正确");
            return 0;
        }
//        注册之前判断次QQ邮箱是否绑定了其他邮箱
        User user1 = registerMapper.selectByEmail(user.getEmail());
        if (user1 != null) {
//            return "次邮箱已经被绑定了";
            log.info("次邮箱已经被绑定了");

            return 0;
        } else {
//        没有绑定进行注册
            int registry = registerMapper.registry(user);
            log.info("注册成功");
//            return "注册成功";
            return registry;
        }

    }

    @Override
    public String login(User user) {
        User login = registerMapper.login(user);
//        log.info("==========="+login.toString());
        if (login!=null){
            return "登录成功";
        }
        return "账号或者密码错误";
    }
}
